package com.sfac.javaSpringBoot.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sfac.javaSpringBoot.modules.account.dao.UserDao;
import com.sfac.javaSpringBoot.modules.account.entity.User;
import com.sfac.javaSpringBoot.modules.account.service.UserService;
import com.sfac.javaSpringBoot.modules.test.entity.Result;
import com.sfac.javaSpringBoot.modules.test.entity.Search;
import com.sfac.javaSpringBoot.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 在Java开发中，通常将后台分成几层，常见的三层mvc：model、view,controller，模型+视图+控制层三层，
 * 而impl通常处于controller层的service下，用来存放接口的实现类，impl的全称是implement，表示实现的意思
 *
 * 对UserService接口中的抽象类进行重写实现
 */


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional
    public Result<User> insertUser(User user) {
        // 设置当前时间
        user.setCreateDate(LocalDateTime.now());
        // 密码加密
        user.setPassword(MD5Util.getMD5(user.getPassword()));
        // 判断 userName 或者 email 是否存在
        List<User> users = Optional
                .ofNullable(
                        userDao.getUsersByUserNameOrEmail(user.getUserName(), user.getEmail())
                ).orElse(Collections.emptyList());

        // 如果存在则返回错误信息
        if (users.size() > 0) {
            return new Result<>(
                    Result.ResultCode.FAILED.code, "User name or email repeat.");
        }
        // 不存在则进行插入操作，并返回正确信息
        userDao.insertUser(user);
        return new Result<>(
                Result.ResultCode.SUCCESS.code, "Insert success.", user);
    }

    @Override
    @Transactional
    public Result<User> updatetUser(User user) {
        // 判断 userName 或者 email 是否存在
        List<User> users = Optional
                .ofNullable(
                        userDao.getUsersByUserNameOrEmail(user.getUserName(), user.getEmail())
                ).orElse(Collections.emptyList());

        if (users.size() > 0 && users.get(0).getId() != user.getId()) {
            return new Result<>(
                    Result.ResultCode.FAILED.code, "User name or email repeat.");
        }

        userDao.updateUser(user);
        return new Result<>(
                Result.ResultCode.SUCCESS.code, "Update success.", user);
    }

    @Override
    public User getUserById(int id) {
        return userDao.getUserById(id);
    }

    @Override
    public Result<User> getUserByUserNameAndPassword(String userName, String password) {
        User user = userDao.getUserByUserNameAndPassword(userName,MD5Util.getMD5((password)));
        if (user == null){
            return new Result<>(
                    Result.ResultCode.FAILED.code,"用户名或密码输入错误，请重新输入.");
        }
        return new Result<>(
                Result.ResultCode.SUCCESS.code,"Login success.",user);
    }//下一步，进入控制层UserController

    @Override
    public Result<Object> deleteUserById(int id) {
        userDao.deleteUserById(id);
        return new Result<>(
                Result.ResultCode.SUCCESS.code, "Delete success");
    }

    @Override
    public PageInfo<User> getUsersBySearch(Search search) {
        PageHelper.startPage(search.getCurrentPage(), search.getPageSize());
        return new PageInfo<>(Optional
                .ofNullable(userDao.getUsersBySearch(search))
                .orElse(Collections.emptyList()));
    }


}